//
//  YHDiscoveryTableCell.m
//  FAKER
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "YHDiscoveryTableCell.h"
#import "AllHeader.h"
#import "FitHeader.h"
#import "Focus.h"

@implementation YHDiscoveryTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
        _manager = [CoreDataManager shareManager];
        [self changeColor];
    }
    return self;
}

#pragma mark - 创建视图
- (void)createViews
{
//    _coverImageView = [[UIImageView alloc] init];
//    [CONTENT_VIEW addSubview:_coverImageView];
    _coverImageView = self.coverImageView;
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:15];
    [CONTENT_VIEW addSubview:_titleLabel];
    
    _detailLabel = [[UILabel alloc] init];
    _detailLabel.font = [UIFont systemFontOfSize:13];
    _detailLabel.textColor = [UIColor grayColor];
    [CONTENT_VIEW addSubview:_detailLabel];
    
    _addButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_addButton addTarget:self action:@selector(clickAddButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [CONTENT_VIEW addSubview:_addButton];
}
- (UIImageView *)coverImageView
{
    if (!_coverImageView) {
        _coverImageView = [[UIImageView alloc] init];
        [CONTENT_VIEW addSubview:_coverImageView];
    }
    return _coverImageView;
}

#pragma mark - 点击加号方法
- (void)clickAddButtonAction
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"focus"];
    
    NSArray *resultArray = [self searchCoreData];
    for (Focus *focus in resultArray) {
        if ([focus.title isEqualToString:_discovery.title]) {
            [_manager.managedObjectContext deleteObject:focus];
            [_manager saveContext];
            [self.delegate yhReloadDataWithStatus:NO];
            return;
        }
    }
    Focus *focus = [NSEntityDescription insertNewObjectForEntityForName:@"Focus" inManagedObjectContext:_manager.managedObjectContext];
    focus.title = _discovery.title;
    focus.api_url = _discovery.api_url;
    focus.pic = _discovery.pic;
    [_manager saveContext];
//    NSLog(@"%@", [_manager applicationDocumentsDirectory]);
    
    //点击动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    basicAnimation.toValue = [NSNumber numberWithInteger:M_PI * 2];
    basicAnimation.duration = .5;
    [_addButton.layer addAnimation:basicAnimation forKey:nil];
    
    [UIView animateWithDuration:.5 animations:^{
        _addButton.alpha = 0;
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.delegate yhReloadDataWithStatus:YES];
        _addButton.alpha = 1;
    });
}

#pragma mark - 布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    _coverImageView.frame = CGRectMake(15 * FITWIDTH, 10 * FITHEIGHT, 50 * FITHEIGHT, 50 * FITHEIGHT);
    _titleLabel.frame = CGRectMake(_coverImageView.X + _coverImageView.WIDTH + 15 * FITHEIGHT, _coverImageView.Y, 225 * FITWIDTH, _coverImageView.HEIGHT / 2);
    _detailLabel.frame = CGRectMake(_titleLabel.X, _titleLabel.Y + _titleLabel.HEIGHT, _titleLabel.WIDTH, _titleLabel.HEIGHT);
    _addButton.frame = CGRectMake(SCREEN_WIDTH - _coverImageView.X - 40 * FITHEIGHT, 0, 40 * FITHEIGHT, 40 * FITHEIGHT);
    _addButton.center = CGPointMake(_addButton.center.x, CONTENT_VIEW.center.y);
}

#pragma mark - 重写属性set
#pragma mark 重写discovery
- (void)setDiscovery:(YHDiscovery *)discovery
{
    _discovery = discovery;
    [self setImageWithFocus];
    
    [_coverImageView sd_setImageWithURL:[NSURL URLWithString:_discovery.pic] placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
    _titleLabel.text = discovery.title;
    _detailLabel.text = discovery.stitle;
}

- (void)setImageWithFocus
{
    NSArray *resultArray = [self searchCoreData];
    for (Focus *focus in resultArray) {
        if ([focus.title isEqualToString:_discovery.title]) {
            [_addButton setBackgroundImage:[UIImage imageNamed:@"SubscriptionBlockCellSelect-0-black@2x"] forState:UIControlStateNormal];
            return;
        }
    }
    [_addButton setBackgroundImage:[UIImage imageNamed:@"DailyHot_favorite_add_dark@2x"] forState:UIControlStateNormal];
}

- (NSArray *)searchCoreData
{
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Focus"];
    return [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
}

#pragma makr - 夜间模式改变颜色
- (void)changeColor
{
    if (NSUSERDEFAULTS_NIGHT) {
        self.backgroundColor = BGCOLOR_BLACK;
        _titleLabel.textColor = [UIColor lightGrayColor];
        _detailLabel.textColor = [UIColor lightGrayColor];
    }
    else {
        self.backgroundColor = BGCOLOR_WHITE;
        _titleLabel.textColor = [UIColor blackColor];
        _detailLabel.textColor = [UIColor grayColor];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
